package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.domain.Mark;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CommodityMapper;
import cn.wolfcode.trip.base.mapper.MarkMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.FollowQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.StringUtil;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private MarkMapper markMapper;

    @Override
    public void register(User user) {
        String email = user.getEmail();
        if (!StringUtil.hasLength(email)) {
            throw new RuntimeException("邮箱不能为空");
        }
        User temp = userMapper.selectUserByEmailAndPassword(email, null);
        if (temp != null) {
            throw new RuntimeException("邮箱已注册!");
        }
        user.setCoverImgUrl("/img/user/bg.jpeg");
        user.setHeadImgUrl("/img/user/head.jpg");
        userMapper.insert(user);
    }

    @Override
    public User login(User user) {
        String email = user.getEmail();
        String password = user.getPassword();
        if (StringUtil.hasLength(email) && StringUtil.hasLength(password)){
            User temp = userMapper.selectUserByEmailAndPassword(email, password);
            if (temp == null) {
                throw new RuntimeException("邮箱或密码有误!");
            }
            UserContext.setCurrentUser(temp);
            return temp;
        }
        throw new RuntimeException("邮箱或密码不能为空!");
    }

    @Override
    public void update(User user) {
        if (user != null) {
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<User>(list);
    }

    @Override
    public User get(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public void follow(Long followId, Long fansId) {
        userMapper.insertFollowRelation(followId,fansId);

    }

    @Override
    public void disFollow(Long followId, Long fansId) {
        userMapper.deleteFollowRelation(followId,fansId);
    }

    @Override
    public void checkFollow(Long followId, Long fansId) {
        User result=userMapper.selectUserByFollowIdAndFansId(followId,fansId);
        if (result == null) {
            throw new RuntimeException("没关注");
        }
    }

    @Override
    public PageInfo<User> queryForFollow(FollowQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectFollowsForList(qo);
        return new PageInfo<User>(list);
    }

    @Override
    public PageInfo<User> queryForFan(FollowQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectFansForList(qo);
        return new PageInfo<User>(list);
    }

    @Override
    public void checkIsBuyByMark(Long id, Long commodityId) {
        //查询商品兑换需要的积分
        Commodity commodity = commodityMapper.selectByPrimaryKey(commodityId);
        Integer markMark = commodity.getMark();
        //查询用户拥有的积分
        Mark mark = markMapper.selectByUserId(id);
        Integer userMark = mark.getMark();
        if(markMark<userMark){
            userMark=userMark-markMark;
            mark.setMark(userMark);
            if(userMark<500){
                mark.setLevel(1);
            }else if(userMark>=500&&markMark<5000){
                mark.setLevel(2);
            }
            else if(userMark>=5000&&markMark<100000){
                mark.setLevel(3);
            }else{
                mark.setLevel(4);
            }
            markMapper.updateByPrimaryKey(mark);
        }else{
            throw new RuntimeException("积分不够,请充值");

        }
    }

    @Override
    public void checkIsBuyByMoney(Long id, Long commodityId) {
        //查询商品购买需要的钱
        Commodity commodity = commodityMapper.selectByPrimaryKey(commodityId);
        Integer price = commodity.getPrice();
        //查询用户的余额
        User user = userMapper.selectByPrimaryKey(id);
        Integer balance = user.getBalance();
        if(balance>price){
            balance=balance-price;
            user.setBalance(balance);
            userMapper.updateByPrimaryKey(user);
        }else{
            throw new RuntimeException("余额不足,请充值后重试");
        }
    }

    @Override
    public User selectBalance(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    /*@Override
    public List<News> getNewsByUserId(Long userId) {
        return userMapper.getNewsByUserId(userId);
    }*/
}
